package com.succodes.lostandfound.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.succodes.lostandfound.dto.PropertyAddEditParam;
import com.succodes.lostandfound.dto.PropertyQueryParam;
import com.succodes.lostandfound.entity.Property;
import com.succodes.lostandfound.mapper.PropertyMapper;
import com.succodes.lostandfound.security.JwtUser;
import com.succodes.lostandfound.service.IPropertyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.succodes.lostandfound.util.JwtUtils;
import com.succodes.lostandfound.vo.PropertyVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;

/**
 * <p>
 * 丢失的个人所有物，包括根据物品寻找失主和失主寻找失物 服务实现类
 * </p>
 *
 * @author 石炫杰
 * @since 2022-08-17
 */
@Service
public class PropertyServiceImpl extends ServiceImpl<PropertyMapper, Property> implements IPropertyService {
    @Resource
    PropertyMapper propertyMapper;

    @Override
    public Page<PropertyVo> appPage(PropertyQueryParam propertyQueryParam) {
            return propertyMapper.appPage(new Page(propertyQueryParam.getCurrent(), propertyQueryParam.getSize()), propertyQueryParam);
    }

    @Override
    public PropertyVo detail(Long propertyId) {
        return propertyMapper.detail(propertyId);
    }

    @Override
    public Boolean add(PropertyAddEditParam propertyAddEditParam) {
        JwtUser jwtUser= JwtUtils.getJwtUser();
        // 添加失物
        Property property=new Property();
        BeanUtils.copyProperties(propertyAddEditParam,property);
        property.setPropertyId(null);
        property.setCreatorId(jwtUser.getUserId());
        property.setCreatorName(jwtUser.getUsername());
        // 默认未完成
        property.setFinished(0);
        save(property);
        return true;
    }
}
